<template>
  <a-modal
    title="新增客户"
    :visible="open"
    @ok="submitForm"
    @cancel="cancel"
    width="700px"
  >
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-row :gutter="32" style="width: 80%; margin-left: 10%">
        <a-col :span="12">
          <a-form-model-item label="客户名称" prop="cusName">
            <a-input v-model="form.cusName" placeholder="请输入客户名称" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="电话" prop="phone">
            <a-input v-model="form.phone" placeholder="请输入电话" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="所在城市(省份-市区)" prop="cityName">
            <a-select
              placeholder="所在城市(省份-市区)"
              v-model="form.cityName"
              style="width: 100%"
              allow-clear
            >
              <a-select-option
                v-for="(d, index) in cityList"
                :key="index"
                :value="d.name.replace('市', '')"
                >{{ d.name }}</a-select-option
              >
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="客户来源" prop="laiyuan">
            <a-select
              placeholder="选择客户来源"
              v-model="form.laiyuan"
              style="width: 100%"
              allow-clear
            >
              <a-select-option
                v-for="(d, index) in busTypeOptions"
                :key="index"
                :value="d.dictValue"
                >{{ d.dictLabel }}</a-select-option
              >
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="申请金额" prop="amount">
            <a-input v-model="form.amount" placeholder="请输入申请金额" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="车牌号码" prop="carNo">
            <a-input v-model="form.carNo" placeholder="请输入车牌号码" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="车辆状态" prop="carStatus">
            <a-select
              style="width: 100%"
              v-model="form.carStatus"
              placeholder="选择车辆状态"
            >
              <a-select-option
                v-for="(item, key) in carStatusOptions"
                :key="key"
                :value="item.dictValue"
              >
                {{ item.dictLabel }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
        </a-col>
        <a-col
          :span="12"
          v-if="form.carStatus == '按揭车' || form.carStatus == '抵押车'"
        >
          <a-form-model-item label="抵押到期日期" prop="carStatus">
            <a-date-picker
              v-model="form.diyaTime"
              value-format="YYYY-MM-DD"
              placeholder="抵押到期日期"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="上牌日期" prop="fuckCarNoTime">
            <a-date-picker
              v-model="form.fuckCarNoTime"
              value-format="YYYY-MM-DD"
              placeholder="上牌日期"
              style="width: 100%"
            />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="车辆品牌" prop="carModel">
            <a-input v-model="form.carModel" placeholder="车辆品牌" />
          </a-form-model-item>
        </a-col>
        <a-col :span="12">
          <a-form-model-item label="评估价格" prop="pgPrice">
            <a-input-number
              v-model="form.pgPrice"
              placeholder="输入评估价格"
              :min="2"
              :max="500"
              :step="0.5"
              :formatter="(value) => `${value}万`"
              :parser="(value) => value.replace('万', '')"
              style="width: 100%"
              :defaultValue="2"
            />
          </a-form-model-item>
        </a-col>

        <a-col :span="24">
          <a-form-model-item label="备注信息" prop="remark">
            <a-textarea v-model="form.remark" :rows="2"></a-textarea>
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>
<script>
import { addBizCustomer } from "@/api/biz/bizCustomer";
import { findListByCode } from "@/api/biz/sysArea";
export default {
  name: "AddCustomerInfoForm",
  data() {
    return {
      carStatusOptions: [],
      busTypeOptions: [],
      // 区域信息
      cityList: [],
      open: false,
      // 表单参数
      form: {},
      rules: {
        cusName: [
          { required: true, message: "客户姓名不能为空", trigger: "blur" },
        ],
        phone: [
          { required: true, message: "客户电话不能为空", trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              // 手机号正则
              const mobileReg = /^1[3-9]\d{9}$/;

              if (mobileReg.test(value)) {
                callback();
              } else {
                callback("请输入正确的电话号码");
              }
            },
          },
        ],
        cityName: [
          { required: true, message: "城市区域不能为空", trigger: "blur" },
        ],
        laiyuan: [{ required: true, message: "来源不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getOptions();
  },
  methods: {
    handleShowModal() {
      this.open = true;
    },
    /** 获取下拉框数据源 */
    getOptions() {
      this.getDicts("car_status_options").then((response) => {
        this.carStatusOptions = response.data;
      });
      this.getDicts("laiyuan_self").then((response) => {
        this.busTypeOptions = response.data;
      });
      findListByCode().then((res) => {
        this.cityList = res.data;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.$emit("close");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          const saveForm = JSON.parse(JSON.stringify(this.form));
          addBizCustomer(saveForm).then((response) => {
            this.$message.success("新增成功", 3);
            this.open = false;
            this.$emit("ok");
            this.$emit("close");
          });
        } else {
          return false;
        }
      });
    },
  },
};
</script>
